package com.atguigu.flink.chapter07.watermark;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;

import java.time.Duration;

/**
 * @Author lzc
 * @Date 2022/7/7 10:23
 */
public class Flink01_Window_Tumbling {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
    
        env
            .socketTextStream("hadoop162", 9999)
            .map(line -> {
                String[] data = line.split(",");
                System.out.println("xxxxx");
                return new WaterSensor(
                    data[0],
                    Long.valueOf(data[1]),
                    Integer.valueOf(data[2])
                );
            })
            .assignTimestampsAndWatermarks(
                WatermarkStrategy
                    .<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                    .withTimestampAssigner(new SerializableTimestampAssigner<WaterSensor>() {
                        // 返回一个毫秒值
                        @Override
                        public long extractTimestamp(WaterSensor element, long recordTimestamp) {
                            return element.getTs();
                        }
                    })
                    .withIdleness(Duration.ofSeconds(10))
            )
            .keyBy(WaterSensor::getId)
            .window(TumblingEventTimeWindows.of(Time.seconds(5)))
            .allowedLateness(Time.seconds(2))  // 最终会真正的关闭.
            /*.process(new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
                // 当时间超过了窗口的结束时间的时候, 会自动触发计算. 方法会执行
                @Override
                public void process(String key,
                                    Context ctx,
                                    Iterable<WaterSensor> elements,  // 存储了这窗口内所有的元素
                                    Collector<String> out) throws Exception {
                    List<WaterSensor> list = new ArrayList<>();
                    for (WaterSensor element : elements) {
                        list.add(element);
                    }
                    out.collect( + " " + new Date(ctx.window().getStart()) + " " + new Date(ctx.window().getEnd()) + "  " + list);
                
                }
            })*/
            .sum("vc")
            .print();
        
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
/*
flink解决数据乱序问题:
1. 事件时间+水印

2. 允许迟到数据

3. 当窗口关闭之后, 把迟到的数据放入到 侧输出流
 */